package com.j2ee.spring.aop.annotation;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @Aspect 说明该类是一个切面类
 * @author LC
 *
 */
/**
 * @Aspect +
 * @Pointcut("execution(* com.j2ee.spring.aop.annotation.PersonDaoImpl.*(..))")
	private void aa() {}
 * ==
 * <aop:config>
 * 		<aop:pointcut
			expression="execution(* com.j2ee.spring.aop.annotation.PersonDaoImpl.*(..))"
			id="aa()" />
 * </aop:config>
 */
@Component("transaction")
@Aspect
public class Transaction {
	
	@Pointcut("execution(* com.j2ee.spring.aop.annotation.PersonDaoImpl.*(..))")
	private void aa() {}
	
	@Before("aa()")
	public void beginTransaction(JoinPoint joinPoint) {
		joinPoint.getArgs() ; //连接点的参数
		joinPoint.getTarget() ;	//目标类
		joinPoint.getSignature().getName() ;	//连接点的名称——客户端调用方法的名称
		System.out.println("begin transaction");
	}
	
	@AfterReturning("aa()")
	public void commit(JoinPoint joinPoint, Object val) {
		System.out.println(val);
		System.out.println("commit");
	}
	
	@After("aa()")
	public void finallyMethod() {
		System.out.println("finally method");
	}
	
	@AfterThrowing("aa()")
	public void throwingMethod(JoinPoint joinPoint, Throwable ex) {
		System.out.println(ex.getMessage());
	}
	
	@Around("aa()")
	public void aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("在目标方法调用之前");
		joinPoint.proceed(); //调用目标方法，与拦截器中的invoke方法类似
		System.out.println("在目标方法调用之后");
	}
	
}
